/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.report.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ce.cetag.model.AjaxSelectModel;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.IChargeItemService;
import com.ce.pms.charge.service.IFeeBankService;
import com.ce.pms.charge.vo.BankVO;
import com.ce.pms.charge.vo.ChargeItemVO;
import com.ce.pms.common.annotation.NoDACL;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IManageAreaService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.PrecinctVO;
import com.sitechasia.webx.core.annotation.Read;

/**
 * 报表Action
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-11-19
 */

public class ReportAction extends PmsBaseAction {
	private IManageAreaService	manageAreaService;

	private IBuildingService	buildingService;

	private ISysrefcodeService sysrefcodeService;

	private IChargeItemService chargeItemService;

	private IFeeBankService feeBankService;

	private final static String	fdfxtime	= "report_fdfxtime";

	private final static String	fdfx		= "report_fdfx";

	private final static String	fxmxtime	= "report_fxmxtime";

	private final static String	fxmx		= "report_fxmx";


	/**
	 * 初始化
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-19
	 */
	public String initMain() {
		return "initMain";
	}

	/**
	 * 跳转到相应名称的报表查询页面
	 *
	 * @param pageName
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-20
	 */
	@NoDACL
	public String jumpToPage(@Read(key = "pageName") String pageName) {
		// 管理区
		getRequest().setAttribute("precinct", precinctList());

		//如果是收费方式明細表，则加载所有的收费方式
		if("report_sffsmx".equals(pageName)){
			this.setPayMethod();
		}
		//如果是银行未扣款，银行已扣款的 则加载银行信息
		if ("report_yhwkk".equals(pageName) || "report_yhykk".equals(pageName)){
			this.setBank();
		}

		//如果是单项费用分栋汇总表，单项费用分户汇总表,则加载收费项目信息
		if("report_dxfyfd".equals(pageName) || "report_dxfyfh".equals(pageName)) {
			this.setChargeType();
		}

		return pageName;
	}

	/**
	 * 功能描述:获取所有的收费方式  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-1  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void setPayMethod(){
		 List payMethods = sysrefcodeService.querySysrefCodesByCode("FEE_BUSINESS_CODE_PAY_METHOD");
		 getRequest().setAttribute("payMethods", payMethods);
	}

	/**
	 * 功能描述:获到所有的银行  <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-12-3  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void setBank(){
		List<BankVO> bankList = feeBankService.getAllBankList();
		getRequest().setAttribute("bankList", bankList);
	}

	/**
	 * 获取所有收费项目
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-3
	 */
	private void setChargeType() {
		List<ChargeItemVO> chargeTypeList =chargeItemService.queryChargeItemList(null, false);
		getRequest().setAttribute("chargeTypeList", chargeTypeList);
	}

	private List<PrecinctVO> precinctList() {
		return manageAreaService.getAllChargePrecinct();
	}

	/**
	 * 级联选择building
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-25
	 */
	@NoDACL
	public void unitedSelect() {
		String precinctId = getRequest().getParameter("selectId");

		List<BuildingVO> buildingVoList = buildingService
				.queryBuildListByPrecinctId(Long.valueOf(precinctId));

		List<AjaxSelectModel> ajaxList = new ArrayList<AjaxSelectModel>();

		//添加楼栋数据集
		for(BuildingVO buildingTmp: buildingVoList) {
			AjaxSelectModel node = new AjaxSelectModel();

			node.setId(buildingTmp.getKeyword().toString());
			node.setText(buildingTmp.getName());

			ajaxList.add(node);

		}

		byte[] bytes;
		String formatMsg = "";
		formatMsg = generateOutputString(ajaxList);
		this.getResponse().setHeader("Cache-Control", "no-cache");
		this.getResponse().setContentType("text/xml");
		try {
			bytes = formatMsg.getBytes("UTF-8");
			this.getResponse().getOutputStream().write(bytes);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	/**
	 * xml回调
	 *
	 * @param dataList
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-11-25
	 */
	private String generateOutputString(List<AjaxSelectModel> dataList) {

		StringBuffer buffer = new StringBuffer();

		if (dataList != null && dataList.size() > 0) {
			buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			buffer.append("<select-list>");
			for (int i = 0; i < dataList.size(); i++) {
				AjaxSelectModel node = (AjaxSelectModel) dataList.get(i);
				buffer.append("<select-node>");
				buffer.append("<option-text>" + node.getText()
						+ "</option-text>");
				buffer.append("<option-id>" + node.getId() + "</option-id>");
				buffer.append("</select-node>");
			}
			buffer.append("</select-list>");
		}

		System.out.println("returnValue---->" + buffer.toString());
		return buffer.toString();
	}

	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}

	public void setFeeBankService(IFeeBankService feeBankService) {
		this.feeBankService = feeBankService;
	}

	/**
	 * @return the buildingService
	 */
	public IBuildingService getBuildingService() {
		return buildingService;
	}

	/**
	 * @param buildingService the buildingService to set
	 */
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @return the manageAreaService
	 */
	public IManageAreaService getManageAreaService() {
		return manageAreaService;
	}

	/**
	 * @param manageAreaService the manageAreaService to set
	 */
	public void setManageAreaService(IManageAreaService manageAreaService) {
		this.manageAreaService = manageAreaService;
	}

	public void setChargeItemService(IChargeItemService chargeItemService) {
		this.chargeItemService = chargeItemService;
	}

}
